<template>
  <div class="table-box">
    <div class="table-search">
      <div class="btn-box">
        <el-button type="primary" plain @click="onAdd">新增</el-button>
      </div>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border ref="tableRef" v-loading="loading">
          <el-table-column prop="id" label="序号" align="center" />
          <el-table-column prop="name" label="客服名称" align="center" />
          <el-table-column
            prop="contact_number"
            label="联系方式"
            align="center"
          />
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="220"
          >
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="handleOperate(scope.$index, scope.row, 'edit')"
                >编辑</el-button
              >
              <el-button
                type="danger"
                @click="handleOperate(scope.$index, scope.row, 'delete')"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <!-- 新增、编辑客服 -->
    <el-dialog
      v-model="dialogFormVisible"
      :destroy-on-close="true"
      title="客服信息"
      width="450px"
      @close="closeDialog"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        :label-width="formLabelWidth"
      >
        <el-form-item label="客服名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="客服联系方式" prop="contact_number">
          <el-input v-model="form.contact_number" />
        </el-form-item>
        <el-form-item label="id" style="display: none">
          <el-input v-model="form.id" disabled />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="submit" :loading="btnLoading">
            提交
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { exportToExcel } from "@/utils/utils";
import {
  customerServiceListApi,
  customerServiceAddApi,
  customerServiceUpdateApi,
  customerServiceDelApi,
} from "@/api/moby/customer_service/customer_service";

const tableRef = ref(null);
const formRef = ref(null);
const dialogFormVisible = ref(false);
const tableData = ref([]);
// 分页
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
// 表单
const formData = {
  id: "",
  name: "",
  contact_number: "",
};
const form = reactive({ ...formData });

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
  };
};

const optType = ref(null);
const loading = ref(false);
const btnLoading = ref(false);
const formLabelWidth = "120px";
// 校验
const rules = {
  name: [{ required: true, message: "请输入客服名称", trigger: "blur" }],
  contact_number: [
    { required: true, message: "请输入客服联系方式", trigger: "blur" },
  ],
};
// 新增
const onAdd = () => {
  dialogFormVisible.value = true;
  optType.value = "add";
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await customerServiceListApi(data);

  let head = {
    id: "id编号",
    name: "客服名称",
    contact_number: "客服联系方式",
  };
  exportToExcel(res.data.results, head, "Mobybox-客服列表");
};
// 关闭
const closeDialog = () => {
  Object.assign(form, formData);
};
// 取消
const cancel = () => {
  dialogFormVisible.value = false;
  Object.assign(form, formData);
};
// 提交
const submit = () => {
  formRef.value
    .validate()
    .then(async () => {
      try {
        btnLoading.value = true;
        if (optType.value == "add") {
          const res = await customerServiceAddApi(form);
          ElMessage({
            message: res.message,
            type: "success",
          });
        } else if (optType.value == "edit") {
          const res = await customerServiceUpdateApi(form);
          ElMessage({
            message: res.message,
            type: "success",
          });
        }

        dialogFormVisible.value = false;
        btnLoading.value = false;
        getTbaleData();
      } catch (error) {
        btnLoading.value = false;
        dialogFormVisible.value = false;
      }
    })
    .catch((error) => {
      console.log(error);
    });
};

// 表格按钮操作
const handleOperate = (index, row, type) => {
  if (type === "edit") {
    optType.value = "edit";
    Object.assign(form, {
      id: row.id,
      name: row.name,
      contact_number: row.contact_number,
    });
    dialogFormVisible.value = true;
  }
  if (type === "delete") {
    ElMessageBox.confirm(`您是否确认删除此客服账号?`, "温馨提示", {
      type: "warning",
    }).then(async () => {
      const res = await customerServiceDelApi({ id: row.id });
      ElMessage.success(res.message);
      getTbaleData();
    });
  }
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await customerServiceListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};

// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>

<style lang="scss" scoped></style>
